/*
 * Copyright (c) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "napi_lua.h"
#include "napi_arkts.h"
#include "napi_common.h"
using namespace std;

lua_State *g_L;

static lua_State* GetLuaStateL() {
    return g_L;
}

static int l2tLogDebug(lua_State *L) {
    DLogInfoVoid("testTag");

    string log = lua_tostring(L, 1);
    OH_LOG_Print(LOG_APP, LOG_INFO, 0, "testTag", "%{public}s", log.c_str());

    return 0;
}

static int l2tLoadModule(lua_State *L) {
    DLogInfoContent("testTag", "触发lua动态加载ts模块");

    string pathModuleTs = lua_tostring(L, 1);
    DLogInfo("testTag", "获取lua参数,pathModuleTs=%{public}s", pathModuleTs.c_str());

    napi_value Result;
    auto status = LoadModule(pathModuleTs, &Result);
    DLogInfo("testTag", "读取模块,结果=%{public}d,pathModuleTs=%{public}s", status,
                 pathModuleTs.c_str());

    return 0;
}

static int l2tCallModuleFunc(lua_State *L) {
    DLogInfoVoid("testTag")

    string pathModuleTs = lua_tostring(L, 1);
    DLogInfo("testTag", "l2tCallModuleFunc,获取lua参数,pathModuleTs=%{public}s",
                 pathModuleTs.c_str());

    string strFunction = lua_tostring(L, 2);
    DLogInfo("testTag", "l2tCallModuleFunc,获取lua参数,pathModuleTs=%{public}s",
                 strFunction.c_str());
    
    napi_value result;
    napi_status status = LoadModule(pathModuleTs, &result);
    DLogInfo("testTag", "l2tCallModuleFunc,读取模块,结果=%{public}d,pathModuleTs=%{public}s",
        status, pathModuleTs.c_str());

    // 2. 使用napi_get_named_property获取info函数
    napi_value infoFn;
    status = napi_get_named_property(g_env, result, strFunction.c_str(), &infoFn);
    DLogInfo("testTag", "状态,结果=%{public}d", status);
    
    int parCount = lua_gettop(L);
    DLogInfo("testTag", "par=%{public}d", parCount);
    napi_value *pArgs = nullptr;
    if (parCount>2)
    {
        pArgs = new napi_value[parCount-2];
    }
    for (int i=3; i<parCount+1; i++)
    {
        if (lua_isnumber(L, i))
        {
            int value = lua_tointeger(L, i); // 将堆栈顶部的值转换为数字

            status = napi_create_int32(g_env, value, &pArgs[i-3]);
            DLogInfo("testTag", "状态,结果=%{public}d", status);
        } else if (lua_isstring(L, i)) {
            string value = lua_tostring(L, i); // 将堆栈顶部的值转换为数字

            status = napi_create_string_utf8(g_env, value.c_str(), value.size(), &pArgs[i-3]);
            DLogInfo("testTag", "状态,结果=%{public}d", status);
        }
    }
    
    napi_value resultCall = nullptr;
    // 3. 使用napi_call_function调用info函数
    status = napi_call_function(g_env, result, infoFn, parCount-2, pArgs, &resultCall);
    DLogInfo("testTag", "状态,结果=%{public}d", status);

    if (resultCall==nullptr) return 0;
    
    napi_valuetype resultType;
    napi_typeof(g_env, resultCall, &resultType);
    switch (resultType) { 
        case  napi_undefined:
            return 0;
        case napi_null:
            return 0;
        case napi_boolean:{
            bool resultBool;
            napi_get_value_bool(g_env, resultCall, &resultBool);
            lua_pushboolean(L, resultBool);
            }
            return 1;
        case napi_number: {
            int resultInt;
            napi_get_value_int32(g_env, resultCall, &resultInt);
            lua_pushinteger(L, resultInt);
        }
            return 1;
        case napi_string: {
            char tmpChar[2048];
            size_t size = sizeof(tmpChar);
            napi_get_value_string_utf8(g_env, resultCall, tmpChar, size, &size);
        }
            return 1;
        case napi_bigint:{
            int64_t resultInt;
            napi_get_value_int64(g_env, resultCall, &resultInt);
            lua_pushnumber(L, resultInt);
        }
            return 1;
        default:
            lua_pushinteger(L, 0);
            return 1;
    }

    return 0;
}

static int l2tCallClass(lua_State *L) {
    DLogInfoVoid("testTag");

    string pathModuleTs = lua_tostring(L, 1);
    DLogInfo("testTag", "l2tCallClass,获取lua参数,pathModuleTs=%{public}s", pathModuleTs.c_str());

    string strClass = lua_tostring(L, 2);
    DLogInfo("testTag", "l2tCallClass,获取lua参数,strClass=%{public}s", strClass.c_str());
    
    string strFunction = lua_tostring(L, 3);
    DLogInfo("testTag", "l2tCallClass,获取lua参数,strFunction=%{public}s", strFunction.c_str());
    
    napi_value result;
    napi_status status = LoadModule(pathModuleTs, &result);
    DLogInfo("testTag", "l2tCallClass,读取模块,结果=%{public}d,pathModuleTs=%{public}s",
        status, pathModuleTs.c_str());

    // 2. 使用napi_get_named_property获取info函数
    napi_value infoClass;
    status = napi_get_named_property(g_env, result, strClass.c_str(), &infoClass);
    DLogInfo("testTag", "状态,结果=%{public}d", status);
    
    // 2. 使用napi_get_named_property获取info函数
    napi_value infoFn;
    status = napi_get_named_property(g_env, infoClass, strFunction.c_str(), &infoFn);
    DLogInfo("testTag", "状态,结果=%{public}d", status);
    
    int parCount = lua_gettop(L);
    DLogInfo("testTag", "par=%{public}d", parCount);
    napi_value *pArgs = nullptr;
    if (parCount>2)
    {
        pArgs = new napi_value[parCount-2];
    }
    for (int i = 4; i < parCount+1; i++)
    {
        if (lua_isnumber(L, i))
        {
            int value = lua_tointeger(L, i); // 将堆栈顶部的值转换为数字

            status = napi_create_int32(g_env, value, &pArgs[i-4]);
            DLogInfo("testTag", "状态,结果=%{public}d", status);
        } else if (lua_isstring(L, i)) {
            string value = lua_tostring(L, i); // 将堆栈顶部的值转换为数字

            napi_value ntag;
            status = napi_create_string_utf8(g_env, value.c_str(), value.size(), &pArgs[i-4]);
            DLogInfo("testTag", "状态,结果=%{public}d", status);
        }
    }
    
    napi_value resultCall=nullptr;
    // 3. 使用napi_call_function调用info函数
    status = napi_call_function(g_env, result, infoFn, parCount-3, pArgs, &resultCall);
    DLogInfo("testTag", "状态,结果=%{public}d", status);

    if (resultCall==nullptr) return 0;
    
    napi_valuetype resultType;
    napi_typeof(g_env, resultCall, &resultType);
    switch (resultType) {
        case  napi_undefined:
            return 0;
        case napi_null:
            return 0;
        case napi_boolean:{
            bool resultBool;
            napi_get_value_bool(g_env, resultCall, &resultBool);
            lua_pushboolean(L, resultBool);
            }
            return 1;
        case napi_number: {
            int resultInt;
            napi_get_value_int32(g_env, resultCall, &resultInt);
            lua_pushinteger(L, resultInt);
            }
            return 1;
        case napi_string: {
            char tmpChar[2048];
            size_t size = sizeof(tmpChar);
            napi_get_value_string_utf8(g_env, resultCall, tmpChar, size, &size);
            }
            return 1;
        case napi_bigint:{
            int64_t resultInt;
            napi_get_value_int64(g_env, resultCall, &resultInt);
            lua_pushnumber(L, resultInt);
            }
            return 1;
        default:
            lua_pushinteger(L, 0);
            return 1;
    }

    return 0;
}

static int l2tSetModuleVarInt(lua_State *L) {
    DLogInfoVoid("testTag l2tSetModuleVarInt,触发lua动态加载ts模块");

    string pathModuleTs = lua_tostring(L, 1);
    DLogInfo("testTag", "l2tSetModuleVarInt,获取lua参数,pathModuleTs=%{public}s", pathModuleTs.c_str());

    string keyStr = lua_tostring(L, 2);
    DLogInfo("testTag", "l2tSetModuleVarInt,获取lua参数,pathModuleTs=%{public}s", keyStr.c_str());    
    
    int keyValue = lua_tointeger(L, 3);
    DLogInfo("testTag", "l2tSetModuleVarInt,获取lua参数,keyValue=%{public}d", keyValue);

    napi_value result;
    auto status = LoadModule(pathModuleTs, &result);
    DLogInfo("testTag", "l2tSetModuleVarInt,读取模块,结果=%{public}d,pathModuleTs=%{public}s",
        status, pathModuleTs.c_str());
    
    // 创建一个表示int型整数的napi_value
    napi_value jsValue;
    status = napi_create_int32(g_env, keyValue, &jsValue);
    if (status == napi_ok) {
        DLogInfo("testTag", "l2tSetModuleVarInt,创建int型的napi_value成功, status= %{public}d", status);
    } else {
        DLogInfo("testTag", "l2tSetModuleVarInt,创建int型的napi_value失败, status= %{public}d", status);
    }
    // 设置属性及值
    status = napi_set_named_property(g_env, result, keyStr.c_str(), jsValue);
        if (status == napi_ok) {
        DLogInfo("testTag", "l2tSetModuleVarInt,设置属性成功, status= %{public}d", status);
    } else {
        DLogInfo("testTag", "l2tSetModuleVarInt,设置属性失败, status= %{public}d", status);
    }
    return 1;
}

static int l2tSetModuleVarLong(lua_State *L) {
    DLogInfoVoid("testTag l2tSetModuleVarLong,触发lua动态加载ts模块");

    string pathModuleTs = lua_tostring(L, 1);
    DLogInfo("testTag", "l2tSetModuleVarLong,获取lua参数,pathModuleTs=%{public}s",
        pathModuleTs.c_str());

    string keyStr = lua_tostring(L, 2);
    DLogInfo("testTag", "l2tSetModuleVarLong,获取lua参数,pathModuleTs=%{public}s", keyStr.c_str());

    int keyValue = lua_tointeger(L, 3);
    DLogInfo("testTag", "l2tSetModuleVarLong,获取lua参数,keyValue=%{public}d", keyValue);

    napi_value result;
    auto status = LoadModule(pathModuleTs, &result);
    DLogInfo("testTag", "l2tSetModuleVarLong,读取模块,结果=%{public}d,pathModuleTs=%{public}s",
        status, pathModuleTs.c_str());

    // 创建一个表示long型整数的napi_value
    napi_value jsValue;
    status = napi_create_int64(g_env, keyValue, &jsValue);
    if (status == napi_ok) {
        DLogInfo("testTag", "l2tSetModuleVarLong,创建long型的napi_value成功, status= %{public}d", status);
    } else {
        DLogInfo("testTag", "l2tSetModuleVarLong,创建long型的napi_value失败, status= %{public}d", status);
    }
    // 设置属性及值
    status = napi_set_named_property(g_env, result, keyStr.c_str(), jsValue);
    if (status == napi_ok) {
        DLogInfo("testTag", "l2tSetModuleVarLong,设置属性成功, status= %{public}d", status);
    } else {
        DLogInfo("testTag", "l2tSetModuleVarLong,设置属性失败, status= %{public}d", status);
    }
    return 1;
}

static int l2tSetModuleVarChar(lua_State *L) {
    DLogInfoVoid("testTag l2tSetModuleVarChar,触发lua动态加载ts模块");

    string pathModuleTs = lua_tostring(L, 1);
    DLogInfo("testTag", "l2tSetModuleVarChar,获取lua参数,pathModuleTs=%{public}s", pathModuleTs.c_str());

    string keyStr = lua_tostring(L, 2);
    DLogInfo("testTag", "l2tSetModuleVarChar,获取lua参数,pathModuleTs=%{public}s", keyStr.c_str());

    string keyValue = lua_tostring(L, 3);
    DLogInfo("testTag", "l2tSetModuleVarChar,获取lua参数,keyValue=%{public}s", keyValue.c_str());

    napi_value result;
    auto status = LoadModule(pathModuleTs, &result);
    DLogInfo("testTag",
        "l2tSetModuleVarChar,读取模块,结果=%{public}d,pathModuleTs=%{public}s", status, pathModuleTs.c_str());

    // 创建一个表示char型整数的napi_value
    napi_value jsValue;
    status = napi_create_string_utf8(g_env, keyValue.c_str(), keyValue.size(), &jsValue);
    if (status == napi_ok) {
        DLogInfo("testTag", "l2tSetModuleVarChar,创建char型的napi_value成功, status= %{public}d", status);
    } else {
        DLogInfo("testTag", "l2tSetModuleVarChar,创建char型的napi_value失败, status= %{public}d", status);
    }
    // 设置属性及值
    status = napi_set_named_property(g_env, result, keyStr.c_str(), jsValue);
    if (status == napi_ok) {
        DLogInfo("testTag", "l2tSetModuleVarChar,设置属性成功, status= %{public}d", status);
    } else {
        DLogInfo("testTag", "l2tSetModuleVarChar,设置属性失败, status= %{public}d", status);
    }
    return 1;
}

static int l2tSetModuleVarDouble(lua_State *L) {
    DLogInfoVoid("testTag l2tSetModuleVarDouble,触发lua动态加载ts模块");

    string pathModuleTs = lua_tostring(L, 1);
    DLogInfo("testTag", "l2tSetModuleVarDouble,获取lua参数,pathModuleTs=%{public}s", pathModuleTs.c_str());

    string keyStr = lua_tostring(L, 2);
    DLogInfo("testTag", "l2tSetModuleVarDouble,获取lua参数,pathModuleTs=%{public}s", keyStr.c_str());

    double keyValue = lua_tonumber(L, 3);
    DLogInfo("testTag", "l2tSetModuleVarDouble,获取lua参数,keyValue=%{public}f", keyValue);

    napi_value result;
    auto status = LoadModule(pathModuleTs, &result);
    DLogInfo("testTag",
        "l2tSetModuleVarDouble,读取模块,结果=%{public}d,pathModuleTs=%{public}s", status, pathModuleTs.c_str());

    // 创建一个表示double型整数的napi_value
    napi_value jsValue;
    status = napi_create_double(g_env, keyValue, &jsValue);
    if (status == napi_ok) {
        DLogInfo("testTag", "l2tSetModuleVarDouble,创建double型的napi_value成功, status= %{public}d", status);
    } else {
        DLogInfo("testTag", "l2tSetModuleVarDouble,创建double型的napi_value失败, status= %{public}d", status);
    }
    // 设置属性及值
    status = napi_set_named_property(g_env, result, keyStr.c_str(), jsValue);
    if (status == napi_ok) {
        DLogInfo("testTag", "l2tSetModuleVarDouble,设置属性成功, status= %{public}d", status);
    } else {
        DLogInfo("testTag", "l2tSetModuleVarDouble,设置属性失败, status= %{public}d", status);
    }
    return 1;
}

static int l2tSetModuleVarBool(lua_State *L) {
    DLogInfoVoid("testTag l2tSetModuleVarBool,触发lua动态加载ts模块");

    string pathModuleTs = lua_tostring(L, 1);
    DLogInfo("testTag", "l2tSetModuleVarBool,获取lua参数,pathModuleTs=%{public}s", pathModuleTs.c_str());

    string keyStr = lua_tostring(L, 2);
    DLogInfo("testTag", "l2tSetModuleVarBool,获取lua参数,pathModuleTs=%{public}s", keyStr.c_str());

    bool keyValue = lua_toboolean(L, 3);
    DLogInfo("testTag", "l2tSetModuleVarBool,获取lua参数,keyValue=%{public}d", keyValue);

    napi_value result;
    auto status = LoadModule(pathModuleTs, &result);
    DLogInfo("testTag", "l2tSetModuleVarBool,读取模块,结果=%{public}d, pathModuleTs=%{public}s",
        status, pathModuleTs.c_str());

    // 创建一个表示bool型整数的napi_value
    napi_value jsValue;
    status = napi_create_int32(g_env, keyValue, &jsValue);
    if (status == napi_ok) {
        DLogInfo("testTag", "l2tSetModuleVarBool,创建bool型的napi_value成功, status= %{public}d", status);
    } else {
        DLogInfo("testTag", "l2tSetModuleVarBool,创建bool型的napi_value失败, status= %{public}d", status);
    }
    // 设置属性及值
    status = napi_set_named_property(g_env, result, keyStr.c_str(), jsValue);
    if (status == napi_ok) {
        DLogInfo("testTag", "l2tSetModuleVarBool,设置属性成功, status= %{public}d", status);
    } else {
        DLogInfo("testTag", "l2tSetModuleVarBool,设置属性失败, status= %{public}d", status);
    }
    return 1;
}

static int l2tGetModuleVarInt(lua_State *L) {
    DLogInfoVoid("testTag l2tGetModuleVarInt,触发lua动态加载ts模块");

    string pathModuleTs = lua_tostring(L, 1);
    DLogInfo("testTag", "l2tGetModuleVarInt,获取lua参数,pathModuleTs=%{public}s",
                 pathModuleTs.c_str());

    string keyStr = lua_tostring(L, 2);
    DLogInfo("testTag", "l2tGetModuleVarInt,获取lua参数,keyStr=%{public}s",
                 keyStr.c_str());

    napi_value result;
    auto status = LoadModule(pathModuleTs, &result);
    DLogInfo("testTag", "l2tGetModuleVarInt,读取模块,结果=%{public}d,pathModuleTs=%{public}s",
        status, pathModuleTs.c_str());

    napi_value value;
    napi_value key;
    napi_create_string_utf8(g_env, keyStr.c_str(), keyStr.size(), &key);
    // 4. 使用napi_get_property获取变量value
    napi_get_property(g_env, result, key, &value);

    int32_t tmpVal;
    status = napi_get_value_int32(g_env, value, &tmpVal);
    if (status == napi_ok) {
        DLogInfo("testTag", "l2tGetModuleVarInt,获取int变量成功, value= %{public}d", tmpVal);

    } else {
        DLogInfo("testTag", "l2tGetModuleVarInt,获取int变量失败, status= %{public}d", tmpVal);
    }

    lua_pushnumber(L, tmpVal);

    return 1;
}

static int l2tGetModuleVarLong(lua_State *L) {
    DLogInfoVoid("testTag l2tGetModuleVarLong,触发lua动态加载ts模块");

    string pathModuleTs = lua_tostring(L, 1);
    DLogInfo("testTag", "l2tGetModuleVarLong,获取lua参数,pathModuleTs=%{public}s",
                 pathModuleTs.c_str());

    string keyStr = lua_tostring(L, 2);
    DLogInfo("testTag", "l2tGetModuleVarLong,获取lua参数,keyStr=%{public}s",
                 keyStr.c_str());

    napi_value result;
    auto status = LoadModule(pathModuleTs, &result);
    DLogInfo("testTag",
                 "l2tGetModuleVarLong,读取模块,结果=%{public}d,pathModuleTs=%{public}s", status, pathModuleTs.c_str());

    napi_value value;
    napi_value key;
    napi_create_string_utf8(g_env, keyStr.c_str(), keyStr.size(), &key);
    // 4. 使用napi_get_property获取变量value
    napi_get_property(g_env, result, key, &value);

    DLogInfoContent("testTag", "l2tGetModuleVarLong,获取key成功");

    int64_t tmpVal;
    status = napi_get_value_int64(g_env, value, &tmpVal);
    if (status == napi_ok) {
        DLogInfo("testTag", "l2tGetModuleVarLong,获取long变量成功, value= %{public}ld",
                     tmpVal);

    } else {
        DLogInfo("testTag", "l2tGetModuleVarLong,获取long变量失败, status= %{public}d",
                     status);
    }

    lua_pushinteger(L, tmpVal);

    return 1;
}

static int l2tGetModuleVarChar(lua_State *L) {
    DLogInfoVoid("testTag l2tGetModuleVarChar,触发lua动态加载ts模块");

    string pathModuleTs = lua_tostring(L, 1);
    DLogInfo("testTag", "l2tGetModuleVarChar,获取lua参数,pathModuleTs=%{public}s", pathModuleTs.c_str());

    string keyStr = lua_tostring(L, 2);
    DLogInfo("testTag", "l2tGetModuleVarChar,获取lua参数,keyStr=%{public}s", keyStr.c_str());

    napi_value result;
    auto status = LoadModule(pathModuleTs, &result);
    DLogInfo("testTag", "l2tGetModuleVarChar,读取模块,结果=%{public}d,pathModuleTs=%{public}s",
        status, pathModuleTs.c_str());

    napi_value value;
    napi_value key;
    napi_create_string_utf8(g_env, keyStr.c_str(), keyStr.size(), &key);
    
    // 4. 使用napi_get_property获取变量value
    napi_get_property(g_env, result, key, &value);

    char tmpChar[1024];
    size_t size = sizeof(tmpChar);
    napi_get_value_string_utf8(g_env, value, tmpChar, size, &size);
    if (status == napi_ok) {
        DLogInfo("testTag", "l2tGetModuleVarChar,获取char变量成功, value= %{public}s", tmpChar);

    } else {
        DLogInfo("testTag", "l2tGetModuleVarChar,获取char变量失败, status= %{public}d", status);
    }

    lua_pushlstring(L, tmpChar, size);

    return 1;
}

static int l2tGetModuleVarDouble(lua_State *L) {
    DLogInfoVoid("testTag l2tGetModuleVarDouble,触发lua动态加载ts模块");

    string pathModuleTs = lua_tostring(L, 1);
    DLogInfo("testTag", "l2tGetModuleVarDouble,获取lua参数,pathModuleTs=%{public}s", pathModuleTs.c_str());

    string keyStr = lua_tostring(L, 2);
    DLogInfo("testTag", "l2tGetModuleVarDouble,获取lua参数,keyStr=%{public}s", keyStr.c_str());

    napi_value result;
    auto status = LoadModule(pathModuleTs, &result);
    DLogInfo("testTag", "l2tGetModuleVarDouble,读取模块,结果=%{public}d,pathModuleTs=%{public}s",
        status, pathModuleTs.c_str());

    napi_value value;
    napi_value key;
    napi_create_string_utf8(g_env, keyStr.c_str(), keyStr.size(), &key);
    // 4. 使用napi_get_property获取变量value
    napi_get_property(g_env, result, key, &value);

    double tmpVal;
    status = napi_get_value_double(g_env, value, &tmpVal);
    if (status == napi_ok) {
        DLogInfo("testTag", "l2tGetModuleVarDouble,获取double类型变量成功, value= %{public}lf", tmpVal);

    } else {
        DLogInfo("testTag", "l2tGetModuleVarDouble,获取double类型变量失败, status= %{public}d", status);
    }

    lua_pushnumber(L, tmpVal);
    return 1;
}

static int l2tGetModuleVarBool(lua_State *L) {
    DLogInfoVoid("testTag l2tGetModuleVarBool,触发lua动态加载ts模块");

    string pathModuleTs = lua_tostring(L, 1);
    DLogInfo("testTag", "l2tGetModuleVarBool,获取lua参数,pathModuleTs=%{public}s",
                 pathModuleTs.c_str());

    string keyStr = lua_tostring(L, 2);
    DLogInfo("testTag", "l2tGetModuleVarBool,获取lua参数,keyStr=%{public}s", keyStr.c_str());

    napi_value result;
    auto status = LoadModule(pathModuleTs, &result);
    DLogInfo("testTag",
                 "l2tGetModuleVarBool,读取模块,结果=%{public}d,pathModuleTs=%{public}s", status, pathModuleTs.c_str());

    napi_value value;
    napi_value key;
    napi_create_string_utf8(g_env, keyStr.c_str(), keyStr.size(), &key);
    // 4. 使用napi_get_property获取变量value
    napi_get_property(g_env, result, key, &value);

    bool tmpBool;
    napi_get_value_bool(g_env, value, &tmpBool);
    if (status == napi_ok) {
        DLogInfo("testTag",
                     "l2tGetModuleVarBool,获取bool类型变量成功, value= %{public}d", tmpBool);

    } else {
        DLogInfo("testTag",
                     "l2tGetModuleVarBool,获取bool类型变量失败, status= %{public}d", status);
    }

    lua_pushboolean(L, tmpBool);

    return 1;
}

static int l2tTestHelloWorld(lua_State *L) {
    DLogInfoContent("testTag", "hello world!");
    return 0;
}

static int l2cTestCallBack(lua_State *L) {
    DLogInfoVoid("testTag");

    const char *pStrFunction = lua_tostring(L, 1);

    lua_getglobal(L, pStrFunction); // 获取函数，压入栈中
    int iRet = lua_pcall(L, 0, 0, 0); // 调用函数，调用完成以后，会将返回值压入栈中，2表示参数个数，1表示返回结果个数。
    if (iRet) {
        const char *pErrorMsg = lua_tostring(L, -1);
        DLogInfo("testTag", "调用失败str=%{public}s", pErrorMsg);
    } else {
        DLogInfoContent("testTag", "调用成功end");
    }
    return 0;
}

static const struct luaL_Reg myLib[] = {
    {"l2tLogDebug", l2tLogDebug},                     // 调用c++日志接口,info
    {"l2tTestHelloWorld", l2tTestHelloWorld},         // 测试helloworld
    {"l2tLoadModule", l2tLoadModule},                 // 加载arkts模块
    {"l2tCallModuleFunc", l2tCallModuleFunc},         // 请求arkts模块函数
    //华为接口开发人员确认,ts变量不能直接修改
    //{"l2tSetModuleVarInt", l2tSetModuleVarInt},       // 设置arkts模块变量-int类型
    //{"l2tSetModuleVarLong", l2tSetModuleVarLong},       // 设置arkts模块变量-long类型
    //{"l2tSetModuleVarChar", l2tSetModuleVarChar},       // 设置arkts模块变量-char类型
    //{"l2tSetModuleVarDouble", l2tSetModuleVarDouble},       // 设置arkts模块变量-double类型
    //{"l2tSetModuleVarBool", l2tSetModuleVarBool},       // 设置arkts模块变量-bool类型
    {"l2tGetModuleVarInt", l2tGetModuleVarInt},       // 获取arkts模块变量-int类型
    {"l2tGetModuleVarLong", l2tGetModuleVarLong},     // 获取arkts模块变量-long类型
    {"l2tGetModuleVarChar", l2tGetModuleVarChar},     // 获取arkts模块变量-char类型
    {"l2tGetModuleVarDouble", l2tGetModuleVarDouble}, // 获取arkts模块变量-double类型
    {"l2tGetModuleVarBool", l2tGetModuleVarBool},     // 获取arkts模块变量-bool类型
    {"l2tCallClass", l2tCallClass},                   // 测试动态加载系统库里的模块并调用
    {"l2cTestCallBack", l2cTestCallBack},             // 测试c++直接回调
    
    {NULL, NULL}                                      // 数组中最后一对必须是{NULL, NULL}，用来表示结束
};

int luaopen_mLualib(lua_State *L) {
    luaL_register(L, "cc", myLib); // 注册导出类
    return 1;                      // 把myLib表压入了栈中，所以就需要返回1
}